package com.etogc.http;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.etogc.groupchat.common.CommonSp;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZeroVoid on 2018-3-14.
 */

public class MultipartRequest<T> extends Request<T> {

    private Gson mGson = new Gson();
    private Class mClazz = null;
    private Response.Listener<T> mListener = null;
    private Map mHeaders = null;
    private String mMimeType = null;
    private byte[] mMultipartBody = null;

    public MultipartRequest(
            String url,
            byte[] multipartBody,
            Response.Listener<T> listener,
            Response.ErrorListener errorListener) {
        super(Method.POST, url, errorListener);
//        this.mClazz = clazz;
//        this.mHeaders = headers;
        this.mListener = listener;
        this.mMultipartBody = multipartBody;
    }

//    public MultipartRequest(String url, Class clazz, Map headers,
//                            Response.Listener listener,
//                            Response.ErrorListener errorListener,
//                            String mimeType,
//                            byte[] multipartBody) {
//        this(Method.POST, url, headers, mimeType, multipartBody, listener, errorListener);
//    }

    @Override
    protected Response parseNetworkResponse(NetworkResponse networkResponse) {
        try {
            String json = new String(networkResponse.data,
                    HttpHeaderParser.parseCharset(networkResponse.headers));
            return Response.success(json, HttpHeaderParser.parseCacheHeaders(networkResponse));
        } catch (Exception e) {
            return Response.error(new ParseError(e));
        }
    }

    @Override
    protected void deliverResponse(T response) {
        mListener.onResponse(response);
    }

    String BOUNDARY = "56423498738365";

//    @Override
//    public byte[] getBody() throws AuthFailureError {
//        if (mListItem == null || mListItem.size() == 0) {
//            return super.getBody();
//        }
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        int N = mListItem.size();
//        FormText formText;
//        for (int i = 0; i < N; i++) {
//            formText = mListItem.get(i);
//            StringBuffer sb = new StringBuffer();
//            /*第一行:"--" + boundary + "\r\n" ;*/
//            sb.append("--" + BOUNDARY);
//            sb.append("\r\n");
//            /*第二行:"Content-Disposition: form-data; name="参数的名称"" + "\r\n" ;*/
//            sb.append("Content-Disposition: form-data;");
//            sb.append("name=\"");
//            sb.append(formText.getName());
//            sb.append("\"");
//            sb.append("\r\n");
//            /*第三行:"\r\n" ;*/
//            sb.append("\r\n");
//            /*第四行:"参数的值" + "\r\n" ;*/
//            sb.append(formText.getValue());
//            sb.append("\r\n");
//            try {
//                bos.write(sb.toString().getBytes("utf-8"));
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        /*结尾行:"--" + boundary + "--" + "\r\n" ;*/
//        String endLine = "--" + BOUNDARY + "--" + "\r\n";
//        try {
//            bos.write(endLine.toString().getBytes("utf-8"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        Log.v("zgy", "=====formText====\n" + bos.toString());
//        return bos.toByteArray();
//        return mMultipartBody;
//    }

    @Override
    public String getBodyContentType() {
////        return "multipart/form-data;boundary=apiclient-" + System.currentTimeMillis();
        return "multipart/form-data;boundary=--------------------" + BOUNDARY;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        mHeaders = new HashMap();
        mHeaders.put("token", CommonSp.getToken());
        mHeaders.put("Accept", "image/gif, image/jpeg, image/pjpeg");
//        mHeaders.put("Content-Type", "multipart/form-data;boundary=--------------------56423498738365");
//        mHeaders.put("WWW-Authenticate", "xBasic realm=\"\"");
        return (mHeaders != null) ? mHeaders : super.getHeaders();
    }

}
